package cn.lxh.user.mapper;

import cn.lxh.basic.mapper.BaseMapper;
import cn.lxh.user.domain.WxPublicContent;
import cn.lxh.user.domain.WxPublicTouser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface WxPublicTouserMappper extends BaseMapper<WxPublicTouser> {

    /**
     *  批量查询
     * @param users
     * @return
     */
    @Select("<script>" +
            "select id , touser_id touserId,name,age,sex,birthday,constellation,city,create_time createTime,update_time updateTime from  wx_public_touser"
            +" <where>" +
            "            id in (\n" +
            "            <foreach collection=\"users\"  item=\"id\" index=\"index\" separator=\",\">\n" +
            "                #{id}\n" +
            "            </foreach>\n" +
            "            )\n" +
            " </where>"
           + "</script>")
    List<WxPublicTouser> selectByToUsers(@Param("users") List<String> users);

    /**
     *
     * @return
     */
    @Select("<script>" +
            "select id , touser_id touserId,name,age,sex,birthday,constellation,city,create_time createTime,update_time updateTime from  wx_public_touser "
            + "</script>")
    List<WxPublicTouser> selectAll();

    /**
     *
     * @param birthday
     * @return
     */
    @Select("<script>" +
            "SELECT DATEDIFF(DATE_FORMAT(birthday, CONCAT(YEAR(NOW()), '-%m-%d')), NOW()) AS days_difference\n" +
            "FROM wx_public_touser\n" +
            "WHERE touser_id = #{touserId}  "
            + "</script>")
    Integer dateDiff(@Param("birthday") String birthday,@Param("touserId") String touserId);

    /**
     *
     * @param wxPublicContent
     */
    @Insert("<script>" +
            "INSERT INTO wx_public_content (template_id,touser_id,first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelfth,thirteenth,fourteenth,fifteenth)" +
            " VALUES (#{wxPublicContent.template_id},#{wxPublicContent.touser_id},#{wxPublicContent.first},#{wxPublicContent.second},#{wxPublicContent.third},#{wxPublicContent.fourth},#{wxPublicContent.fifth},#{wxPublicContent.sixth},#{wxPublicContent.seventh}," +
            "#{wxPublicContent.eighth},#{wxPublicContent.ninth},#{wxPublicContent.tenth},#{wxPublicContent.eleventh},#{wxPublicContent.twelfth},#{wxPublicContent.thirteenth},#{wxPublicContent.fourteenth},#{wxPublicContent.fifteenth})"
            + "</script>")
    void inserIntoContent(@Param("wxPublicContent") WxPublicContent wxPublicContent);


    /**
     *
     * @param templateId
     * @param touserId
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM wx_public_content \n" +
            "WHERE  touser_id = #{touserId} and  template_id =#{templateId} "
            + "</script>")
    WxPublicContent selectContentByTemUser(@Param("templateId") String templateId, @Param("touserId") String touserId);

    /**
     *
     * @param wxPublicContent
     */
    @Update("<script>" +
            "UPDATE wx_public_content \n" +
            "SET first = #{wxPublicContent.first},second=#{wxPublicContent.second},third=#{wxPublicContent.third},fourth=#{wxPublicContent.fourth},fifth=#{wxPublicContent.fifth},sixth=#{wxPublicContent.sixth},seventh=#{wxPublicContent.seventh},eighth=#{wxPublicContent.eighth},ninth=#{wxPublicContent.ninth},tenth=#{wxPublicContent.tenth},eleventh=#{wxPublicContent.eleventh},twelfth=#{wxPublicContent.twelfth},thirteenth=#{wxPublicContent.thirteenth},fourteenth=#{wxPublicContent.fourteenth},fifteenth=#{wxPublicContent.fifteenth} "
            + "WHERE touser_id = #{wxPublicContent.touser_id} and template_id = #{wxPublicContent.template_id}"
            + "</script>")
    void updateIntoContent(@Param("wxPublicContent") WxPublicContent wxPublicContent);
}
